package myJs.implicits

import cats.effect.IO
import cats.effect.unsafe.implicits.global
import myJs.myPkg.jquery._

import scala.scalajs.js
import scala.scalajs.js.JSConverters._
import myJs.Implicits._
import myJs.myPkg.{BootstrapValidator, Select2Options}
import org.scalajs.dom.{Element, Event}

/**
 * Created by Administrator on 2020/1/16
 */
trait MyJqueryTool {

  implicit class MyJquery(jq: JQuery) {

    def myDataStr(attr:String)={
      jq.data(attr).get.toString
    }

    def getFilterDataFieldName= {
      jq.attr("filter-data-field").get
    }

    def myDataDb(attr:String)={
      jq.data(attr).get.toString.toDouble
    }

    def myDataInt(attr:String)={
      jq.data(attr).get.toString.toInt
    }

    def myVal = {
      if (jq.`val`() == null || js.isUndefined(jq.`val`())) {
        ""
      } else jq.`val`().toString
    }

    def myVals = {
      jq.mapElems(y => y).map { y =>
        $(y).myVal
      }.toJSArray
    }

    def myVisVals = {
      jq.mapElems(y => y).map { y =>
        $(y).myVisVal
      }.toJSArray
    }

    def myVisVal = {
      jq.children().mapElems { y =>
        $(y).attr("value").get
      }.mkString(",")
    }

    def select2Val = {
      jq.select2("data").asInstanceOf[js.Array[JDJAn]].map { dict =>
        dict("id").toString
      }
    }

    def select2Val(vs: js.Array[String]) = {
      jq.`val`(vs).trigger("change")
    }

    def select2Val(vs: js.Array[String], parentJq: JQuery) = {
      jq.`val`(vs).select2(Select2Options(dropdownParent=parentJq))
    }

    def select2Val(v: String) = {
      jq.`val`(v).select2()
    }

    def myCss(propertyName: String) = {
      myCsses(propertyName).headOption.getOrElse("")
    }

    def myCsses(propertyName: String) = {
      jq.mapElems { y =>
        $(y).css(propertyName)
      }
    }

    def findInputByName(name: String) = {
      jq.find(s":input[name='${name}']")
    }

    def findInputByValue(value: String) = {
      jq.find(s":input[value='${value}']")
    }

    def nameAttr = {
      jq.attr("name").getOrElse("")
    }

    def setChecked(checked: Boolean) = {
      jq.prop("checked", checked)
    }

    def isShow = {
      jq.css("display") == "block"
    }

    def outerHTML = {
      jq.prop("outerHTML")
    }

    def outerHTMLSome = {
      jq.prop("outerHTML").get.toString
    }

    def dataValue = {
      jq.attr("data-value").get
    }

    def dataValue(v:String) = {
      jq.attr("data-value",v)
    }

    def dataValid(v: Boolean) = {
      jq.attr("data-value", v)
    }

    def dataValid = {
      jq.attr("data-value").get.toBoolean
    }

    def dataInputName = {
      jq.attr("data-inputName").get
    }

    def dataMissionKind = {
      jq.attr("data-missionKind").get
    }

    def dataModal = {
      jq.attr("data-modal").get
    }

    def dataKind = {
      jq.attr("data-kind").getOrElse("")
    }

    def dataId = {
      jq.attr("data-id").get
    }

    def dataRow = {
      jq.attr("data-row").get
    }

    def dataBeforeValue = {
      jq.attr("data-beforeValue").getOrElse("")
    }

    def dataName = {
      jq.attr("data-name").get
    }

    def dataDataInputName = {
      jq.attr("data-dataInputName").getOrElse("")
    }

    def dataColumnNumLeast = {
      jq.attr("data-columnNumLeast").getOrElse("")
    }

    def dataFolder = {
      jq.attr("data-folder").get
    }

    def dataFileName = {
      jq.attr("data-fileName").getOrElse("")
    }

    def title = {
      jq.attr("title").get
    }

    def isChecked = {
      jq.prop("checked").get.toString.toBoolean
    }

    def isVisible = {
      jq.is(":visible")
    }

    def siblingsWithSelf = {
      jq.parent().children()
    }

    def isDisplay = {
      val displayAttrOp = jq.attr("display")
      val noVisible = displayAttrOp.isDefined && displayAttrOp.get == "none"
      !noVisible
    }

    def setDisabled(disabled: Boolean) = {
      jq.attr("disabled", disabled)
    }

    def bv = {
      jq.data("bootstrapValidator").get.asInstanceOf[BootstrapValidator]
    }

    def mySelect2(options: Select2Options) = {
      if (jq.data("select2").isDefined) {
        jq.select2("destroy")
      }
      jq.empty().select2(options)
    }

    def size = {
      jq.length
    }

    def exist = {
      jq.size > 0
    }

    def myOn(events: String, handler: js.Function1[Event, IO[Unit]]) = {
      val useHandler = (e: Event) => handler(e).unsafeRunAndForget()
      jq.on(events, useHandler)
    }

    def myOn(events: String, handler: js.ThisFunction1[Element, Event, IO[Unit]]) = {
      val useHandler = (ele: Element, eve: Event) => handler(ele, eve).unsafeRunAndForget()
      jq.on(events, useHandler)
    }

    def myOffThenOn(events: String, handler: js.Function1[Event, IO[Unit]]) = {
      val useHandler = (e: Event) => handler(e).unsafeRunAndForget()
      jq.off(events).on(events, useHandler)
    }


  }

}
